/****************************************************
    文件：ResSvc.cs
	作者：kk
    邮箱: 627515104@qq.com
    日期：2020/8/26 14:47:53
	功能：Nothing
*****************************************************/

using System;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ResSvc : MonoBehaviour
{
    public static ResSvc Instance = null;
    
    public void InitSvc()
    {
        Debug.Log("Init ResSvc...");
        Instance = this;
        InitRDNameCfg();
    }

    private Action prgCB = null;
    public void AsyncLoadScene(string sceneName,Action loaded)
    {
        GameRoot.Instance.LoadingWnd.SetWndState();
   
        AsyncOperation sceneAsync= SceneManager.LoadSceneAsync(sceneName);

      prgCB = () =>
      {
          float val = sceneAsync.progress;
          GameRoot.Instance.LoadingWnd.SetProgress(val);
          if (val == 1)
          {
              if (loaded != null)
              {
                  loaded();
              }
              sceneAsync = null;
              prgCB = null;
              GameRoot.Instance.LoadingWnd.SetWndState(false);
             
              // LoginSys.Instance.OpenLoginWnd();
          }
      };

    }

    private void Update()
    {
        if (prgCB!=null)
        {
            prgCB();
        }
    }

    private  Dictionary<string,AudioClip> adDic=new Dictionary<string, AudioClip>();

    public AudioClip LoadAudio(string path,bool cache=false)
    {
        AudioClip au = null;
        if (!adDic.TryGetValue(path, out au))
        {
            au = Resources.Load<AudioClip>(path);
            if (cache)
            {
                adDic.Add(path,au);
            }
        }

        return au;
    }


    #region InitCfgs
    private  List<string> surnameList=new List<string>();
    private List<string> manList = new List<string>();
    private List<string> womanList = new List<string>();

    private void InitRDNameCfg()
    {
        TextAsset xml = Resources.Load<TextAsset>(PathDefine.RDNameCfg);

        if (!xml)
        {
            Debug.Log("xml no");
        }
        else
        {
            XmlDocument doc=new XmlDocument();
            doc.LoadXml(xml.text);

            XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
            for (int i = 0; i <nodeList.Count ; i++)
            {
                XmlElement ele=nodeList[i] as  XmlElement;
                if (ele.GetAttributeNode("ID")==null)
                {
                    continue;
                }
                int ID = Convert.ToInt32(ele.GetAttributeNode("ID").InnerText);

                foreach (XmlElement e in nodeList[i].ChildNodes)
                {
                    switch (e.Name)
                    {
                        case "surname":
                            surnameList.Add(e.InnerText);
                            break;

                        case "man":
                            manList.Add(e.InnerText);
                            break;
                        case "woman":
                            womanList.Add(e.InnerText);
                            break;
                    }
                }
            }
        }
    }

    public string GetRDNameData(bool man = true)
    {
        System.Random rd = new System.Random();
        string rdName = surnameList[PETools.RDInt(0, surnameList.Count - 1)];
        if (man)
        {
            rdName += manList[PETools.RDInt(0, manList.Count - 1)];
        }
        else
        {
            rdName += womanList[PETools.RDInt(0, womanList.Count - 1)];
        }

        return rdName;
    }

    #endregion
}